/**
 * Copyright (c) 2010 BullionXpress.com,
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */
package com.bullionxpress.client.view;

import java.rmi.RemoteException;
import java.text.DecimalFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import com.bullionxpress.client.application.ClientGlobals;
import com.bullionxpress.shared.model.Instrument;
import com.bullionxpress.shared.model.Position;
import com.bullionxpress.shared.model.User;
import com.bullionxpress.shared.service.InstrumentDatabase;
import com.bullionxpress.shared.service.PositionNotFoundException;
import com.bullionxpress.shared.service.PositionService;
import com.bullionxpress.shared.util.Log;

/**
 *
 * @author BullionXpress.com
 */
public class RequestDeliveryPanel extends javax.swing.JPanel {

    JDialog parent = null;

    /** Creates new form RequestDelivery */
    public RequestDeliveryPanel(JDialog parent) {
        initComponents();
        this.parent = parent;
        try {
            //
            positionDropBox.removeAllItems();
            PositionService db = ClientGlobals.positionService;
            //db.connect();

            InstrumentDatabase idb = ClientGlobals.instrumentDatabase;
            idb.connect();
            Instrument[] ins = idb.getAll();
            //double costPerUnit = 10;
            for (int i = 0; i < ins.length; i++) {
                String symbol = ins[i].getSymbol();
                Position[] positions = db.getPositionListByAccount(ClientGlobals.account.getId());

                String strValue = null;
                int rt = 0;
                for (int j = 0; j < positions.length; j++) {
                    Instrument instrument = positions[j].getInstrument();
                    String symb = instrument.getSymbol();
                    if( symb.equals(symbol)) {
                        rt += positions[j].getUnits();
                    }
                }
                if( rt != 0 ){
                    strValue = rt + " unit(s) of " + symbol;
                    positionDropBox.addItem(strValue);
                    /* call back
                     * DecimalFormat df = new DecimalFormat("$#,##0.00");
                     * String v = df.format( costPerUnit * rt);
                     * deliveryEstimate.setText(v);
                     */
                }
            }

            ///////////////////
            this.shippingAddress.removeAllItems();

            User user = ClientGlobals.user;

            String address = user.getFirstName() + " " + user.getLastName() + ", " + user.getAddress() + ", " + user.getCity() + ", " + user.getCountry() + ", " + user.getPostalCode(); //...
            this.shippingAddress.addItem(address);


        } catch (PositionNotFoundException ex) {
            Logger.getLogger(RequestDeliveryPanel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (RemoteException ex) {
            Log.info(ex);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jOptionPane1 = new javax.swing.JOptionPane();
        buttonGroup1 = new javax.swing.ButtonGroup();
        requestDeliveryButton = new javax.swing.JButton();
        cancelDeliveryButton = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        positionLabel = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        deliveryEstimate = new javax.swing.JTextField();
        positionDropBox = new javax.swing.JComboBox();
        iAgreeCheckbox = new javax.swing.JCheckBox();
        jPanel2 = new javax.swing.JPanel();
        shippingAddress = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jCheckBox1 = new javax.swing.JCheckBox();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        specialInstructions = new javax.swing.JTextArea();
        radioPickUp = new javax.swing.JRadioButton();
        radioDeliver = new javax.swing.JRadioButton();

        requestDeliveryButton.setText("Request Delivery");
        requestDeliveryButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                requestDeliveryButtonActionPerformed(evt);
            }
        });

        cancelDeliveryButton.setText("Cancel");
        cancelDeliveryButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelDeliveryButtonActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Deliver Position"));

        positionLabel.setText("Position:");

        jLabel3.setText("Estimate");

        deliveryEstimate.setEnabled(false);

        positionDropBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(positionLabel)
                        .addGap(18, 18, 18)
                        .addComponent(positionDropBox, 0, 213, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addGap(18, 18, 18)
                        .addComponent(deliveryEstimate, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(positionLabel)
                    .addComponent(positionDropBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(deliveryEstimate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        iAgreeCheckbox.setText("I Agree to Terms and Conditions");

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Procurement Details"));

        shippingAddress.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel1.setText("Shipping Address");

        jCheckBox1.setSelected(true);
        jCheckBox1.setText("Insurance");
        jCheckBox1.setEnabled(false);

        jLabel4.setText("Special Instructions");

        specialInstructions.setColumns(20);
        specialInstructions.setRows(5);
        jScrollPane1.setViewportView(specialInstructions);

        buttonGroup1.add(radioPickUp);
        radioPickUp.setText("Pick-up");

        buttonGroup1.add(radioDeliver);
        radioDeliver.setText("Deliver");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 272, Short.MAX_VALUE)
                    .addComponent(jLabel1)
                    .addComponent(shippingAddress, 0, 272, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addComponent(radioDeliver)
                        .addGap(18, 18, 18)
                        .addComponent(radioPickUp)
                        .addGap(16, 16, 16))
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jCheckBox1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(shippingAddress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 9, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(radioPickUp)
                        .addComponent(radioDeliver))
                    .addComponent(jCheckBox1, javax.swing.GroupLayout.Alignment.TRAILING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(iAgreeCheckbox)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(cancelDeliveryButton, javax.swing.GroupLayout.DEFAULT_SIZE, 144, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(requestDeliveryButton, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(8, 8, 8)
                .addComponent(iAgreeCheckbox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(requestDeliveryButton)
                    .addComponent(cancelDeliveryButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void cancelDeliveryButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelDeliveryButtonActionPerformed
        // TODO add your handling code here:
        parent.dispose();
    }//GEN-LAST:event_cancelDeliveryButtonActionPerformed

    private void requestDeliveryButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_requestDeliveryButtonActionPerformed
        // TODO add your handling code here:
        if( !iAgreeCheckbox.isSelected() ){
            JOptionPane.showMessageDialog(parent, "You must agree to the terms of service");
        }
        JOptionPane.showMessageDialog(parent, "This request could not currently be processed online. Please contact broker through email with the details of this request.");
    }//GEN-LAST:event_requestDeliveryButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton cancelDeliveryButton;
    private javax.swing.JTextField deliveryEstimate;
    private javax.swing.JCheckBox iAgreeCheckbox;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JOptionPane jOptionPane1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JComboBox positionDropBox;
    private javax.swing.JLabel positionLabel;
    private javax.swing.JRadioButton radioDeliver;
    private javax.swing.JRadioButton radioPickUp;
    private javax.swing.JButton requestDeliveryButton;
    private javax.swing.JComboBox shippingAddress;
    private javax.swing.JTextArea specialInstructions;
    // End of variables declaration//GEN-END:variables
}
